package cn.virens.exception;

/**
 * 自定义数据异常
 * 
 * @作者 :loioi
 * @创建时间 :2016年12月24日
 */
public class APIException extends Exception {

	private static final long serialVersionUID = 7595477192082994944L;

	private String code;
	private String message;

	public APIException(Throwable cause) {
		this(null, null, cause);
	}

	public APIException(int code, String msg) {
		this(Integer.toString(code), msg, null);
	}

	public APIException(String code, String msg) {
		this(code, msg, null);
	}

	public APIException(int code, String message, Throwable cause) {
		this(Integer.toString(code), message, cause);
	}

	public APIException(String code, String message, Throwable cause) {
		this.setCode(code);
		this.setMessage(message);
		// 将源异常填充到当前异常
		if (cause != null) {
			super.initCause(cause);

			if (cause instanceof APIException) {
				this.setCode(((APIException) cause).getCode());
				this.setMessage(((APIException) cause).getMessage());
			}
		}
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getCode() {
		return code;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public String getMessage() {
		if (this.message == null) {
			return super.getMessage();
		} else {
			return message;
		}
	}
}
